
import java.util.*;
import javax.swing.*;

/** This class creates the Action Box for an Anomaly in the
 * {@link DecisionFrame} whenever the suggestion chosen by User is an
 * Anomaly.
 *
 * @author  Gary Morris, Northern Virginia Community College		garymorris2245@verizon.net
 *
 * Created on June 24, 2011, 3:21:57 PM
 */
public class ActionAnomaly extends JPanel {

    DecisionFrame papa;
    Anomaly anna;
    int suggNmbr;
    DomainTheory dt;  //  instantiated by DecisionFrame
    ArrayList<Dyad> oddballs = new ArrayList<Dyad>(), 
                    dyadsProcessed = new ArrayList<Dyad>();
    boolean chartEditPending = false;
    int oldNDX = 0;

    /** Creates new form ActionAnomaly */
    public ActionAnomaly() {
        initComponents();
    }

    public ActionAnomaly(DecisionFrame frame) {
        initComponents();
        papa = frame;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        oddballLabel = new javax.swing.JLabel();
        oddballDyadsCombo = new javax.swing.JComboBox();
        egoLabel = new javax.swing.JLabel();
        egoNames = new javax.swing.JLabel();
        alterLabel = new javax.swing.JLabel();
        alterNames = new javax.swing.JLabel();
        noActionBtn = new javax.swing.JRadioButton();
        dyadCorrectBtn = new javax.swing.JRadioButton();
        dyadDeleteBtn = new javax.swing.JRadioButton();
        editBtn = new javax.swing.JRadioButton();
        correctedKinTerm = new javax.swing.JTextField();
        recordBtn = new javax.swing.JButton();
        showOnChartBtn = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Action Box: Anomaly"));

        oddballLabel.setText("Questionable Dyads");

        oddballDyadsCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Ego:  12  Alter:   6   Kin Term:  grandpaw", "Ego:    5  Alter:  14  Kin Term:  grampa" }));
        oddballDyadsCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                oddballDyadsComboActionPerformed(evt);
            }
        });

        egoLabel.setText("Ego: ");

        egoNames.setText("______");

        alterLabel.setText("Alter: ");

        alterNames.setText("______");

        buttonGroup1.add(noActionBtn);
        noActionBtn.setSelected(true);
        noActionBtn.setText("No Action");

        buttonGroup1.add(dyadCorrectBtn);
        dyadCorrectBtn.setText("This dyad is CORRECT.");

        buttonGroup1.add(dyadDeleteBtn);
        dyadDeleteBtn.setText("This dyad is wrong.  DELETE it.");

        buttonGroup1.add(editBtn);
        editBtn.setText("EDIT the kin term to ");
        editBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editBtnActionPerformed(evt);
            }
        });

        recordBtn.setText("RECORD Decision on This Dyad");
        recordBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                recordBtnActionPerformed(evt);
            }
        });

        showOnChartBtn.setText("SHOW This Dyad on the Chart");
        showOnChartBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showOnChartBtnActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(dyadCorrectBtn)
                        .addContainerGap())
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(oddballLabel)
                        .add(144, 144, 144))
                    .add(layout.createSequentialGroup()
                        .add(oddballDyadsCombo, 0, 399, Short.MAX_VALUE)
                        .addContainerGap())
                    .add(layout.createSequentialGroup()
                        .add(egoLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(egoNames)
                        .addContainerGap(329, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(alterLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(alterNames)
                        .addContainerGap(328, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(noActionBtn)
                        .addContainerGap(321, Short.MAX_VALUE))
                    .add(dyadDeleteBtn)
                    .add(layout.createSequentialGroup()
                        .add(recordBtn)
                        .addContainerGap(174, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(showOnChartBtn)
                            .add(layout.createSequentialGroup()
                                .add(editBtn)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(correctedKinTerm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 181, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                        .add(50, 50, 50))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(oddballLabel)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(oddballDyadsCombo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(egoLabel)
                    .add(egoNames))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(alterLabel)
                    .add(alterNames))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(noActionBtn)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(dyadCorrectBtn)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(dyadDeleteBtn)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(editBtn)
                    .add(correctedKinTerm, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(showOnChartBtn)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(recordBtn)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    public void load(Anomaly a, int nmbr) {
        anna = a;
        suggNmbr = nmbr;
        loadOddballs();
        resetOddballComboBox();
        oddballDyadsCombo.setSelectedIndex(0);
        loadNames(0);
    }

    void loadOddballs() {
        oddballs.clear();
        for (Object o : anna.findOddballs()) {
            Dyad d = (Dyad)o;
            oddballs.add(d);
        }
    }
    
    void resetOddballComboBox() {
        String[] dyStrings = new String[oddballs.size()];
        String pad1, pad2;
        for (int i = 0; i < oddballs.size(); i++) {
            Dyad d = oddballs.get(i);
            String done = (dyadsProcessed.contains(d) ? "DONE: " : "");
            String s = done + "Ego: " + d.ego.serialNmbr;
            s += "  Alter: " + d.alter.serialNmbr;
            s += "  Kin Term: " + d.kinTerm;
            dyStrings[i] = s;
        }
        oddballDyadsCombo.setModel(new DefaultComboBoxModel(dyStrings));
    }
    
    void reviewBeforeDyad() {
        Dyad before = oddballs.get(oldNDX);
        Dyad after = getDyad(before);
        // after == null means 'before' has been modified or deleted
        // otherwise, it is unchanged
        if (after == null) {
            dyadsProcessed.add(before);
        }
    }


    Dyad getDyad(Dyad seed) {
        DyadTMap[] tmaps = { dt.dyadsUndefined, dt.dyadsDefined};      
        for (DyadTMap tm : tmaps) {
            ArrayList<Object> list = null;
            TreeMap subTree = (TreeMap)tm.get(seed.kinTerm);
            if (subTree != null) {
                list = (ArrayList<Object>)subTree.get(seed.pcString);
            }
            if (list != null) {
                for (Object o : list) {
                    Dyad d = (Dyad)o;
                    if (d.ego == seed.ego && d.alter == seed.alter) {
                        return d;
                    }
                }
            }
        }
        return null;
    }
    
    
    String makePad(int num) {
        if (num < 10) {
            return "  ";
        } else if (num < 100) {
            return " ";
        } else {
            return "";
        }
    }

    void loadNames(int num) {
        Dyad d = (Dyad)oddballs.get(num);
        egoNames.setText(d.ego.firstNames + " " + d.ego.surname);
        alterNames.setText(d.alter.firstNames + " " + d.alter.surname);
        correctedKinTerm.setText("");
        correctedKinTerm.setEditable(false);
        noActionBtn.setSelected(true);
    }

    private void recordBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_recordBtnActionPerformed
        if (chartEditPending) {
            chartEditPending = false;
            reviewBeforeDyad();
            resetOddballComboBox();
            oddballDyadsCombo.setSelectedIndex(oldNDX);
            oldNDX = 0;
            return;
        }
        Dyad dy = (Dyad)oddballs.get(oddballDyadsCombo.getSelectedIndex());
        if (editBtn.isSelected()) {            
            int egoNum = dy.ego.serialNmbr,
                altNum = dy.alter.serialNmbr;
            boolean distinct = dt.addressTerms;
            String oldTerm = dy.kinTerm,
                   newTerm = correctedKinTerm.getText();
            Dyad dy2 = new Dyad(dy);
            dy2.kinTerm = newTerm;
            dy2.confirmed = true;
            Context.current.deleteDyad(dt, oldTerm, dy.pcString, egoNum, altNum);
            Context.current.addDyad(dt, dy2);
            Context.current.ktm.correctKinTerm(egoNum, altNum, oldTerm, newTerm, distinct);
            if (!dyadsProcessed.contains(dy)) {
               dyadsProcessed.add(dy); 
            }
            dy.kinTerm = newTerm;
            resetOddballComboBox();
        }else if (dyadCorrectBtn.isSelected()) {
            dy.confirmed = true;
            dy.challenged = false;
            dyadsProcessed.add(dy);
            resetOddballComboBox();
        }else if (dyadDeleteBtn.isSelected()) {
            int egoNum = dy.ego.serialNmbr,
                altNum = dy.alter.serialNmbr;
            boolean distinct = dt.addressTerms;
            Context.current.deleteDyad(dt, dy.kinTerm, dy.pcString, egoNum, altNum);
            Context.current.ktm.deleteKinTerm(egoNum, altNum, dy.kinTerm, distinct);
            dyadsProcessed.add(dy);
            dy.kinTerm = "DELETED";
            resetOddballComboBox();
        }
        // Check to see if last oddball has been processed.
        if (dyadsProcessed.size() == oddballs.size()) {
            papa.markProcessed(suggNmbr);
            papa.reset();
        }
    }//GEN-LAST:event_recordBtnActionPerformed

    private void editBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editBtnActionPerformed
        correctedKinTerm.setEditable(true);
    }//GEN-LAST:event_editBtnActionPerformed

    private void oddballDyadsComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_oddballDyadsComboActionPerformed
        int ndx = oddballDyadsCombo.getSelectedIndex();
        loadNames(ndx);
    }//GEN-LAST:event_oddballDyadsComboActionPerformed

    private void showOnChartBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showOnChartBtnActionPerformed
        chartEditPending = true;
        oldNDX = oddballDyadsCombo.getSelectedIndex();
        Dyad before = oddballs.get(oldNDX);
        String[] pendingModel = { "After viewing/editing chart, choose action, then 'Record'." };
        oddballDyadsCombo.setModel(new DefaultComboBoxModel(pendingModel));
        dyadCorrectBtn.setSelected(true);      
        SIL_Edit.edWin.changeEgo(before.ego.serialNmbr);
        SIL_Edit.edWin.getPPanel().resetEgoBox(before.ego.serialNmbr);
        SIL_Edit.edWin.chart.setAlter(before.alter.serialNmbr);  
        SIL_Edit.edWin.toFront();                                     
    }//GEN-LAST:event_showOnChartBtnActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel alterLabel;
    private javax.swing.JLabel alterNames;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JTextField correctedKinTerm;
    private javax.swing.JRadioButton dyadCorrectBtn;
    private javax.swing.JRadioButton dyadDeleteBtn;
    private javax.swing.JRadioButton editBtn;
    private javax.swing.JLabel egoLabel;
    private javax.swing.JLabel egoNames;
    private javax.swing.JRadioButton noActionBtn;
    private javax.swing.JComboBox oddballDyadsCombo;
    private javax.swing.JLabel oddballLabel;
    private javax.swing.JButton recordBtn;
    private javax.swing.JButton showOnChartBtn;
    // End of variables declaration//GEN-END:variables
}
